﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using Newtonsoft.Json;

namespace NetJsWire.Widgets.Core
{
    public class ListenerJsonConverter : JsonConverter
    {
        #region Overrides of JsonConverter

        /// <summary>
        ///   Writes the JSON representation of the object.
        /// </summary>
        /// <param name = "writer">The <see cref = "T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name = "value">The value.</param>
        /// <param name = "serializer">The calling serializer.</param>
        public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
        {
            var listener = value as Listener;
            
            if ( listener == null )
                return;

            if ( string.IsNullOrEmpty( listener.EventName ) || string.IsNullOrEmpty( listener.Handler ) )
            {
                return;
            }
            //writer.WriteStartObject();
            writer.WritePropertyName( listener.EventName );
            writer.WriteStartObject();
            // Options
            if ( !string.IsNullOrEmpty( listener.Handler ) || !string.IsNullOrEmpty( listener.Scope ) || listener.Options.Buffer > 0 || listener.Options.Delay > 0 || !string.IsNullOrEmpty( listener.Options.Scope ) || !string.IsNullOrEmpty( listener.Options.Target ) || listener.Options.Single )
            {
                writer.WritePropertyName( "fn" );
                writer.WriteRawValue( listener.Handler );
                if ( listener.Options.Buffer > 0 )
                {
                    writer.WritePropertyName( "buffer" );
                    writer.WriteValue( listener.Options.Buffer );
                }

                if ( listener.Options.Delay > 0 )
                {
                    writer.WritePropertyName( "delay" );
                    writer.WriteValue( listener.Options.Delay );
                }

                if ( !string.IsNullOrEmpty( listener.Scope ) )
                {
                    writer.WritePropertyName( "scope" );
                    writer.WriteRawValue( listener.Scope );
                }
                else if ( !string.IsNullOrEmpty( listener.Options.Scope ) )
                {
                    writer.WritePropertyName( "scope" );
                    writer.WriteRawValue( listener.Options.Scope );
                }

                if ( !string.IsNullOrEmpty( listener.Options.Target ) )
                {
                    writer.WritePropertyName( "target" );
                    writer.WriteRawValue( listener.Options.Target );
                }

                if ( listener.Options.Single )
                {
                    writer.WritePropertyName( "single" );
                    writer.WriteValue( listener.Options.Single );
                }
            }
            writer.WriteEndObject();

            writer.Flush();
        }

        /// <summary>
        ///   Reads the JSON representation of the object.
        /// </summary>
        /// <param name = "reader">The <see cref = "T:Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <param name = "objectType">Type of the object.</param>
        /// <param name = "existingValue">The existing value of object being read.</param>
        /// <param name = "serializer">The calling serializer.</param>
        /// <returns>
        ///   The object value.
        /// </returns>
        public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
        {
            return default( Listener );
        }

        public object ReadJson(JsonReader reader, Type objectType, object existingValue)
        {
            return null;
        }

        /// <summary>
        ///   Determines whether this instance can convert the specified object type.
        /// </summary>
        /// <param name = "objectType">Type of the object.</param>
        /// <returns>
        ///   <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanConvert( Type objectType )
        {
            return objectType == typeof( Listener );
        }

        #endregion
    }
}
